import axios from "axios"
import {Notify } from "vant";
import router from '@/router';

// token 就是我们的用户信息

const state = {
    user:{},
};

const mutations = {
    SET_USER(state, payload) {
        state.user = payload;
}
};

const actions = {
    logout(context) {
        Notify({
          message: "已经退出登录",
          type: "success",
        });

        context.commit("SET_USER", {})

        setTimeout(() => {
            router.push("/personal");
          }, 1500);
    },
    
    login(context,payload) {
        //请求数据
        axios({
            url: "http://192.168.10.143:3000/users",
            method: "get",
            params:payload,
        }).then((res) => {
            // console.log(res,"reslogin")
            if (res.data.length) {
                //登录成功
                Notify({
                    type: "success",
                    message: "登陆成功",
                    duration: 1500,
                });

                context.commit("SET_USER", res.data[0]);
               
                setTimeout(() => {
                    router.push("/");
                  }, 1500);
            } else {
                Notify({
                    type: "error",
                    message: "用户名或密码错误",
                    duration:1500,
                })
            }
        })
    }
};

export default {
    //命名空间
    namespaced:true,
    state,
    mutations,
    actions,
}